/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.framework;

import com.jb.web.bean.Image;
import com.jb.web.bean.Page;
import com.jb.web.bean.UIComponentImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public abstract class Registry {
    HashMap hashmap = new HashMap();
    HashMap categories = new HashMap();

    public Map getMap() {
        return hashmap;
    }

    public boolean isRegistered(Object pPropertyName, Object pTestObj) {
        ArrayList list = (ArrayList) hashmap.get(pPropertyName);

        if (list == null) {
            return false;
        }

        for (int i = 0; i < list.size(); i++) {
            if (testRegistration(list.get(i), pTestObj)) {
                return true;
            }
        }

        return false;
    }

    public List getRegisteredValues(Object pPropertyName) {
        return (ArrayList) hashmap.get(pPropertyName);
    }
    
    public void register(Object pPropertyName, Object pClass) {
        ArrayList tmp = (ArrayList) hashmap.get(pPropertyName);

        if (tmp == null) {
            tmp = new ArrayList();
            tmp.add(pClass);
        } else if (!tmp.contains(pClass)) {
            tmp.add(pClass);
        }
        //name -> classes
        hashmap.put(pPropertyName, tmp);
    }

    public static void registerComponents(Page page) {
        //TODO load this from a file
        registerComponent(page, "com.jb.web.bean.custom.GeoMap", "jbPalletteCustom", "GoogleMap", "resources/images/palette/map.jpg");
        registerComponent(page, "com.jb.charts.ChartImpl", "jbPalletteCustom", "Chart", "resources/images/palette/chart.gif");
    }

    public static void registerComponent(Page page, String pClass, String category, String title, String icon) {    
         UIComponentImpl uc=(UIComponentImpl)page.getDescendantById(category);     
         Image img=new Image();
         img.setLocation(icon);
         img.setDestination("${context.pageName}.xsp");
         img.setTitle(title);
         img.setRendered(true);
         img.setStyleClass("toolMenuBarButton");
         img.setRendererType("com.jb.web.renderer.DraggableImageRenderer");
         img.setVisible("true");
         img.setValue(pClass);
         uc.addChild(img);
    }    
    public abstract boolean testRegistration(Object pRegistered, Object pTestObj);
}
